home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 001 / gad / gad.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  12KB  |  358 lines

  1. /**************************************************************************
  2. *     F U N   W I T H   G A D G E T S
  3. *    by John Draper
  4. ***************************************************************************/
  5. #include <exec/types.h>
  6. #include <exec/nodes.h>
  7. #include <exec/lists.h>
  8. #include <exec/ports.h>
  9. #include <exec/devices.h>
  10. #include <devices/keymap.h>
  11. #include <graphics/regions.h>
  12. #include <graphics/copper.h>
  13. #include <graphics/gels.h>
  14. #include <graphics/gfxbase.h>
  15. #include <graphics/gfx.h>
  16. #include <graphics/clip.h>
  17. #include <graphics/view.h>
  18. #include <graphics/rastport.h>
  19. #include <graphics/layers.h>
  20. #include <intuition/intuition.h>
  21. #include <hardware/blit.h>
  22.  
  23. /***************************************************************************
  24.         IMPORTANT CONSTANTS
  25. ***************************************************************************/
  26. unsigned int mask = 0;            /*  ONE BIT SET FOR EACH OPEN */
  27.  
  28. #define  INTUITION   0x00000001
  29. #define  GRAPHICS    0x00000002
  30. #define  SCREEN      0x00000004
  31. #define  WINDOW      0x00000008
  32. #define  COLORMAP    0x00000010
  33. #define  MATH        0x00000020
  34. #define  MATHTRANS   0x00000040
  35.  
  36. #define NL 0
  37.  
  38. /***************************************************************************
  39. *          I N T U I T I O N      G L O B A L      V A R S
  40. ***************************************************************************/
  41.  
  42. struct IntuitionBase *IntuitionBase;
  43. struct GfxBase *GfxBase;
  44. struct IntuiMessage *message;
  45. struct RastPort *rp;
  46. struct Window *w;
  47.  
  48. /***************************************************************************
  49.             G A D G E T S      L I V E      H E R E
  50. ***************************************************************************/
  51.  
  52. /*  Image for a custom proportional gadget */
  53.  
  54. UWORD custimage[] = {
  55. 0x0000, 0x0000, 0x0180, 0x0660, 0x1818, 0x2004, 0x4002, 0x4002,
  56. 0x4002, 0x4002, 0x2004, 0x1818, 0x0660, 0x0180, 0x0000, 0x0000
  57. };
  58.  
  59. struct Image cus_image = {
  60.   0, 0,                    /* LeftEdge, TopEdge */
  61.   16, 16, 1,               /* Width, Height, Depth */
  62.   &custimage[0],           /* Pointer to bit image */
  63.   1,  0,                   /* PlanePick, Planeonoff */
  64.   NULL                     /* No other images */
  65. };
  66.  
  67.  
  68. struct IntuiText rtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"R",NL};
  69. struct IntuiText gtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"G",NL};
  70. struct IntuiText btxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"B",NL};
  71.  
  72. struct IntuiText ntext = {2,2,JAM1, 10, -11, NL,
  73.   (UBYTE *) "Number entry", NL};
  74.  
  75. struct IntuiText cus_text = {2,2,JAM1, 2, -11, NL,
  76.   (UBYTE *) "Custom Prop Gadget", NL};
  77.  
  78. struct IntuiText cus1_txt = {2,2,JAM1, 161, 23, NL,
  79.   (UBYTE *) "Vert side", &cus_text };
  80.  
  81. struct IntuiText cus2_txt = {2,2,JAM1, 20, 50, NL,
  82.   (UBYTE *) "Hor side", &cus1_txt };
  83.  
  84. struct IntuiText bool_str = {2,2,JAM1, 4, 1, NL,
  85.   (UBYTE *) "START", NL };
  86.  
  87. struct IntuiText bool2_str = {2,2,JAM1, 9, 1, NL,
  88.   (UBYTE *) "STOP", NL };
  89.  
  90. struct Image    r_img, g_img, b_img;
  91. struct PropInfo r_prop,g_prop,b_prop;
  92.  
  93. struct PropInfo cust_prop = {
  94.    FREEHORIZ | FREEVERT,            /* Want knob to go both vert and horiz */
  95.    0x8000,  0x8000,                 /* Want knob to be centered initially  */
  96.    0x800,   0x800,                  /* Smallest increment the knob can move */
  97.    150, 50,                         /* cWidth, cHeight - Container w & h */
  98.    1, 1,                            /* HPosres, VPotres - Pot increments */
  99.    0, 0                             /* Container borders  */
  100. };
  101.  
  102.  
  103. #define STRINGSIZE 512
  104. UBYTE DefString[STRINGSIZE] = "000000000000";
  105. UBYTE Undo [STRINGSIZE];
  106.  
  107. struct StringInfo TexString = {
  108.    DefString,                      /* Buffer - Pointer to Buffer */
  109.    Undo,                           /* UndoBuffer - Undo buf ptr  */
  110.    0,                              /* BufferPos - Init Chr Posn */
  111.    STRINGSIZE,                     /* MaxChars - Max number of Chars */
  112.    0, 0,                           /* DispPos - First Disp Chr */
  113.    13,                             /* NumChars - Number of Characters */
  114.    0, 0, 0,                        /* Posn Vars calc by Intuition */
  115.    NULL,                           /* No pointer to Rasport */
  116.    0,                              /* Longint Value */
  117.    NULL                            /* No pointer to alt Keyboard */
  118. };
  119.  
  120. USHORT Pairs[] = {
  121. -1,  -1,                            /* Information describing the */
  122. 160, -1,                           /* border around the gadget */
  123. 160, 9,
  124. -1, 9,
  125. -1, -1
  126.  };
  127.  
  128. USHORT Pairs1[] = {
  129.  0,  0,
  130. 51,  0,
  131. 51, 11,
  132. 0,  11,
  133. 0,  0
  134. }; 
  135.  
  136. #define NUM_PAIRS 5                /* There are Four pairs above */
  137.  
  138. struct Border StrBorder = {
  139.  -1, -1,                           /* LeftEdge, TopEdge */
  140.  1, 0, JAM1,                       /* FrontPen,  BackPen  DrawMode  */
  141.  NUM_PAIRS,                        /* Number of XY Pairs */
  142. (APTR)&Pairs,                      /* XY, Pointer to XY Pairs */
  143.  NULL                              /* No more borders */
  144. };
  145.  
  146. struct Border butt_border = {
  147.  -1, -1,
  148.  1, 0, JAM1,
  149.  NUM_PAIRS,
  150.  (APTR)&Pairs1,
  151.  NULL
  152. };
  153.  
  154. #define BLUE_GADGET 0
  155. struct Gadget blue_gad = {
  156.    NL, 17,112, 150,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  157.    PROPGADGET,(APTR)&b_img, NL,
  158.    &btxt, NL,(APTR)&b_prop, BLUE_GADGET, NL
  159. };
  160.  
  161.  
  162. #define GREEN_GADGET 1        /* Another personal ID */
  163. struct Gadget green_gad = {
  164.    &blue_gad, 17,97, 120,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  165.    PROPGADGET,(APTR)&g_img, NL,
  166.    >xt, NL,(APTR)&g_prop, GREEN_GADGET, NL
  167. };
  168.  
  169.  
  170. #define RED_GADGET 2          /* And yet another gadget */
  171. struct Gadget red_gad = {
  172.    &green_gad, 17,82, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  173.    PROPGADGET,(APTR)&r_img, NL,
  174.    &rtxt, NL,(APTR)&r_prop, RED_GADGET, NL
  175. };
  176.  
  177.  
  178. #define TEX_GAD 3
  179. struct Gadget tex_gad = {
  180.    &red_gad, 30, 30, 150,11, GADGHCOMP, STRINGCENTER | LONGINT | RELVERIFY,
  181.    STRGADGET, (APTR)&StrBorder, NL,
  182.    &ntext, NL, (APTR)&TexString, TEX_GAD, NL
  183. };
  184.  
  185. #define CUST_KNOB 4
  186. struct Gadget cust_knob = {
  187.    &tex_gad, 17, 140, 150, 50, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  188.    PROPGADGET, (APTR)&cus_image, NL,
  189.    &cus2_txt, NL, (APTR)&cust_prop, CUST_KNOB, NL
  190. };
  191.  
  192. #define BOOL_GAD1 5
  193. struct Gadget bool_gad = {
  194.    &cust_knob, 30, 46, 50, 10, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  195.    BOOLGADGET, (APTR)&butt_border, NL,
  196.    &bool_str, NL, NL, BOOL_GAD1, NL
  197. }; 
  198.  
  199. #define BOOL_GAD2 6
  200. struct Gadget bool2_gad = {
  201.   &bool_gad, 30, 60, 50, 10, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  202.   BOOLGADGET, (APTR)&butt_border, NL,
  203.   &bool2_str,  NL, NL, BOOL_GAD2, NL
  204. };
  205.  
  206. /***************************************************************************
  207. *                  N E W     W I N D O W     S T R U C T U R E
  208. ***************************************************************************/
  209.  
  210. struct NewWindow nw = {
  211.   0, 0,                  /*  Start position                               */
  212.   320, 200,              /*  width, height,                               */
  213.   0, 1,                  /*  detail, block pens                           */
  214.   CLOSEWINDOW            /*  IDCMP flags                             */
  215. | REFRESHWINDOW
  216. | MOUSEBUTTONS
  217. | MOUSEMOVE
  218. | GADGETDOWN
  219. | GADGETUP,
  220.                          /*  Regular flags for gadgets and such           */
  221.   WINDOWDEPTH
  222. | WINDOWSIZING
  223. | WINDOWDRAG
  224. | REPORTMOUSE
  225. | WINDOWCLOSE
  226. | SMART_REFRESH,
  227.  
  228.   &bool2_gad,             /* First gadget in list                          */
  229.   NULL,                  /* User checkmark                                */
  230.   "Fun with Gadgets",    /* Window Title                                  */
  231.   NULL,                  /* Pointer to screen (Set later)                 */
  232.   NULL,                  /* Pointer to superbitmap                        */
  233.   0, 0, 320, 186,        /* Ignored because not sizeable                  */
  234.   WBENCHSCREEN,          /* Using the Workbench screen                    */
  235.   };
  236.  
  237. /***************************************************************************
  238.                  M A I N     P R O G R A M     M O D U L E
  239. ***************************************************************************/
  240. main()
  241. {
  242.    ULONG MessageClass;
  243.    USHORT code;
  244.    int good_boy = FALSE;     /*  Be a "Bad boy", and hog the system  **/
  245.  
  246. /***************************************************************************
  247.            Must Initialize the Proportional "Specialinfo" before
  248.       opening up the window.   Init Flags,  and position.
  249. ***************************************************************************/
  250.  
  251.    r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
  252.    r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
  253.    r_prop.HorizPot = g_prop.HorizPot = b_prop.HorizPot = 0x8000;
  254.  
  255. /***************************************************************************
  256.            Read in the ligraries,  and set each "read" mask bit.
  257. ***************************************************************************/
  258.  
  259.    if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
  260.    {
  261.       printf("no graphics library!!!\n");
  262.       close_things();
  263.       exit(1);
  264.    }
  265.    mask |= GRAPHICS;
  266.  
  267.  if(!(IntuitionBase = (struct IntuitionBase *)
  268.        OpenLibrary("intuition.library",0)))
  269.    {
  270.       printf("no intuition here!!\n");
  271.       close_things();
  272.       exit(2);
  273.    }
  274.    mask |= INTUITION;
  275.  
  276. /***************************************************************************
  277.                 OPEN UP THE WINDOW ON TOP OF WORKBENCH SCREEN
  278. ***************************************************************************/
  279.  
  280.    if (!(w = (struct Window *)OpenWindow(&nw) ))
  281.    {
  282.       printf("could not open the window\n");
  283.       close_things();
  284.       exit(3);
  285.    }
  286.    mask |= WINDOW;
  287.  
  288. /***************************************************************************
  289.                  INITIALIZATION BEFORE ENTERING MAIN LOOP
  290. ***************************************************************************/
  291.  
  292.    rp = w->RPort;
  293.    RefreshGadgets(&red_gad,w,NULL);
  294.  
  295. /***************************************************************************
  296.                             MAIN EVENT LOOP
  297. ***************************************************************************/
  298.  
  299.    for (;;)
  300.    {
  301.  
  302.       if (message = (struct IntuiMessage *)GetMsg(w->UserPort))  {
  303.         MessageClass = message->Class;
  304.         code = message->Code;
  305.         ReplyMsg(message);
  306.         switch (MessageClass) {
  307.  
  308.            case GADGETUP    :
  309.            case GADGETDOWN  : do_gadgets(message, w);
  310.                               break;
  311.  
  312.            case CLOSEWINDOW : close_things();
  313.                               exit(0);
  314.                               break;
  315.            case MOUSEBUTTONS: break;
  316.         }   /* Case */
  317.       }  /* if */
  318.    }  /* for */
  319. } /* main */
  320. /***************************************************************************
  321.                            HANDLE THE GADGETS
  322. ***************************************************************************/
  323. do_gadgets (mes, win)
  324.  
  325. struct IntuiMessage *mes;
  326. struct Window *win;
  327. {
  328.         struct Gadget *igad;     /* Ptr to gadget that Intuition found    */
  329.         int  gadgid;             /* ID Code identifying which gadget      */
  330.         ULONG val;
  331.  
  332.      igad = (struct Gadget *) mes->IAddress;      /* Ptr to a gadget      */
  333.         gadgid = igad->GadgetID;    /* My own personal code for this gad  */
  334.         val = (ULONG)TexString.LongInt;
  335.         switch(gadgid) {
  336.  
  337.             case GREEN_GADGET: break;
  338.             case BLUE_GADGET : break;
  339.             case TEX_GAD     : printf("got here ...\n");
  340.                                printf("val = %ld\n", val);
  341.                                break;
  342.         }
  343. }
  344.  
  345.  
  346. /***************************************************************************
  347.                           CLOSE EVERYTHING DOWN
  348. ***************************************************************************/
  349.  
  350. close_things()
  351. {
  352.    if (mask & WINDOW)    CloseWindow(w);
  353.    if (mask & GRAPHICS)  CloseLibrary(GfxBase);
  354.    (void) OpenWorkBench();
  355.    if (mask & INTUITION) CloseLibrary(IntuitionBase);
  356. }
  357.  
  358.